package servicelearn.zx.com.clientapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RemoteViews;

public class RemoteViewLearnActivity extends AppCompatActivity {
    public static String REMOTE_ACTION = "com.zx.servicelearn.remote.action";
    public static String EXTRA_REMOTE_VIEWS = "extra_remote_views";
    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            RemoteViews remoteViews = intent.getParcelableExtra(EXTRA_REMOTE_VIEWS);
            if (remoteViews != null) {
                upDateUI(remoteViews);
            }
        }
    };
    LinearLayout remote_view_ll;

    private void upDateUI(final RemoteViews remoteViews) {
        View view = remoteViews.apply(this, remote_view_ll);
        remote_view_ll.addView(view);
        int id = getResources().getIdentifier("my_pb", "layout", getPackageName());
        remoteViews.setTextViewText(id, "sdada");

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 100; i++) {
//                    remoteViews.setProgressBar(id, 100, i, false);
//                    try {
//                        Thread.sleep(100);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }).start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remote_view_learn);
        remote_view_ll = findViewById(R.id.remote_view_ll);

        IntentFilter filter = new IntentFilter();
        filter.addAction(REMOTE_ACTION);
        registerReceiver(mBroadcastReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        if (mBroadcastReceiver != null)
            unregisterReceiver(mBroadcastReceiver);
        super.onDestroy();


    }
}
